<template>
  <common-edit-dialog
    title="绑定识别规则"
    :visible.sync="bindOpen"
    :button-loading="fieldSubmitLoading"
    @submit="submitbindForm"
    @cancel="bindCancel"
    width="60%"
  >
    <common-search @search="handleQuery" @reset="resetQuery">
      <el-form
        :model="queryParams"
        ref="queryParams"
        :inline="true"
        @submit.native.prevent
        label-width="80px"
      >
        <el-form-item label="规则名称" prop="ruleName">
          <common-input
            v-model="queryParams.ruleName"
            placeholder="请输入"
            @enter="handleQuery"
          ></common-input>
        </el-form-item>
        <el-form-item label="敏感字段" prop="wordNames">
          <el-select v-model="queryParams.wordNames">
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in wordsList"
              :label="item.name"
              :value="item.name"
              :key="item.wordId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </common-search>
    <common-table
      style="height: 316px"
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
      ref="tableList"
      :row-key="'id'"
      isSetRowKey
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        :reserve-selection="true"
      />
      <el-table-column label="规则名称" prop="ruleName" />
      <el-table-column
        label="敏感字段"
        align="left"
        prop="wordNames"
        show-overflow-tooltip
      />
    </common-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </common-edit-dialog>
</template>
<script>
import { queryList } from "@/api/datahandler/discernRule";
import { querySensitiveAllWordsList } from "@/api/datascan/scanresults/column";
export default {
  name: "ruleDialog",

  data() {
    return {
      fieldSubmitLoading: false,
      id: "",
      list: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      bindOpen: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wordNames: "",
        ruleName: "",
        status: 0,
      },
      total: 0,
      loading: false,
      selections: [],
      isChange: false,
      wordsList: [],
    };
  },
  methods: {
    getSensitiveAllWordsList() {
      querySensitiveAllWordsList().then((res) => {
        if (res && res.code === 200) {
          this.wordsList = res.data.dataList;
        } else {
          this.wordsList = [];
        }
      });
    },
    submitbindForm() {
      this.fieldSubmitLoading = true;
      let paramsIds = new Set([...this.ids, ...this.ruleIds]);
      const params = { id: this.id, ruleIds: [...paramsIds].join(",") };
      this.$emit("submitbindForm", params);
    },
    closeLoading() {
      this.fieldSubmitLoading = false;
    },
    bindCancel() {
      this.bindOpen = false;
      this.queryParams.pageNum = 1;
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //重置
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
    },
    handleSelectionChange(selection) {
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      if (this.isChange) {
        this.ids = selection.map((item) => item.id);
        this.selections = [];
        this.selections = new Set(this.ids);
        this.list.map((item) => {
          if (
            ![...this.selections].includes(item.id) &&
            this.ruleIds.includes(item.id)
          ) {
            let index = this.ruleIds.findIndex((t) => t === item.id);
            [...this.selections].splice(index, 1);
            this.ruleIds.splice(index, 1);
          }
        });
      }
    },
    show(row) {
      this.bindOpen = true;
      this.ruleIds = row.ruleIds ? row.ruleIds.split(",") : [];
      this.id = row.typeId;
      this.getList();
      this.getSensitiveAllWordsList();
    },
    getList() {
      this.loading = true;
      queryList({ ...this.queryParams })
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.rows;
            this.isChange = false;
            this.$nextTick(() => {
              this.list.forEach((item) => {
                if (
                  this.ruleIds?.length >= 0 &&
                  this.ruleIds.includes(item.id)
                ) {
                  this.$refs.tableList.$refs.table.toggleRowSelection(
                    item,
                    true
                  );
                }
              });
              this.isChange = true;
            });

            this.total = res.total;
          } else {
            this.list = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>
